﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace MapEngine
{
    public sealed class TileSet
    {
        #region -- Atributos --
        private Texture2D texture;
        private List<TileDef> tiles = new List<TileDef>();
        #endregion



        #region -- Constructores --
        /**
         * <summary></summary>
         * <param name="game"></param>
         * <remarks></remarks>
         */
        public TileSet(Game game, string textureFile, string definitionsFile, int tileWidth, int tileHeight)
        {
            texture = game.Content.Load<Texture2D>(textureFile);
            MapConfig.TileSize.X = tileWidth;
            MapConfig.TileSize.Y = tileHeight;
            //CreateDefaultTiles();

            using (StreamReader reader = new StreamReader(definitionsFile))
            {
                String line;
                while ((line = reader.ReadLine()) != null)
                {
                    String[] fields = line.Split(' ');
                    int row = Convert.ToInt32(fields[0]);
                    int column = Convert.ToInt32(fields[1]);
                    TileMaterial material = (TileMaterial)Enum.Parse(typeof(TileMaterial), fields[2]);
                    String name = fields[3];
                    CreateTile(name, new Point(column, row), material);
                }
            }
        }
        #endregion



        #region -- Gestión de tiles --
        public void CreateTile(string nombre, Point TilePosition, TileMaterial Material = TileMaterial.Floor)
        {
            tiles.Add(new TileDef(nombre, TilePosition, Material));
        }

        /*private void CreateDefaultTiles()
        {
            tiles.Add(new TileDef("Suelo", new Point(0, 0), TileMaterial.Floor));
            tiles.Add(new TileDef("Pared", new Point(0, 1), TileMaterial.Wall));
            tiles.Add(new TileDef("Agua", new Point(0, 2), TileMaterial.Water));
            tiles.Add(new TileDef("Lava", new Point(0, 3), TileMaterial.Fire));
            tiles.Add(new TileDef("Foso", new Point(0, 4), TileMaterial.Hole));
            tiles.Add(new TileDef("Resbaladizo", new Point(0, 5), TileMaterial.Ice));
            tiles.Add(new TileDef("Pegajoso", new Point(0, 6), TileMaterial.Glue));
        }*/
        #endregion



        #region -- Consultas --
        public TileMaterial GetMaterial(int TileNumber)
        {
            return tiles[TileNumber].Material;
        }
        #endregion



        #region -- Métodos de dibujado --
        public void DrawTile(SpriteBatch spriteBatch, int TileNumber, int x, int y, bool masked)
        {
            // Coordenadas del rectángulo de destino
            Rectangle dst = new Rectangle(
                    x * MapConfig.TileSize.X,
                    y * MapConfig.TileSize.Y,
                    MapConfig.TileSize.X,
                    MapConfig.TileSize.Y);

            // Coordenadas del rectángulo de origen
            Rectangle src = new Rectangle(
                tiles[TileNumber].Coords.X * MapConfig.TileSize.X,
                tiles[TileNumber].Coords.Y * MapConfig.TileSize.Y,
                MapConfig.TileSize.X,
                MapConfig.TileSize.Y);

            // Dibuja el tile
            spriteBatch.Draw(
                texture,
                dst,
                src,
                masked ? Color.Red : Color.White,
                0.0f,
                Vector2.Zero,
                SpriteEffects.None,
                0.3f
            );
        }
        #endregion
    }
}
